| Conditions | 16 |
| Total Lines | 92 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like gulp-utils.js ➔ generateContentTasks often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | const utils = require("./utils.js"); |
||
| 174 | function generateContentTasks(dir, extraSources, extensionName, baseTask, destinationWebDir, destinationReleaseDir, zipName) { |
||
| 175 | "use strict"; |
||
| 176 | // Checks which tasks must be generated. If dirs are omitted, tasks are too |
||
| 177 | const executeWebTasks = destinationWebDir !== ""; |
||
| 178 | const executeReleaseTasks = destinationReleaseDir !== ""; |
||
| 179 | |||
| 180 | // Get out of here if no directories are provided |
||
| 181 | if (!executeWebTasks && !executeReleaseTasks) { |
||
| 182 | return {}; |
||
| 183 | } |
||
| 184 | |||
| 185 | const releaseTasks = ["release-do:" + baseTask]; |
||
| 186 | const copyTasks = ["copy-do:" + baseTask]; |
||
| 187 | const watchTasks = ["watch-main:" + baseTask]; |
||
| 188 | const composerExists = fs.existsSync(dir + "/composer.json"); |
||
| 189 | const sources = [dir + "/**"].concat(extraSources); |
||
| 190 | |||
| 191 | if (composerExists) { |
||
| 192 | gulp.task("composer:" + baseTask, function () { |
||
| 193 | return composer({"working-dir": dir}); |
||
| 194 | }); |
||
| 195 | releaseTasks.unshift("composer:" + baseTask); |
||
| 196 | copyTasks.unshift("composer:" + baseTask); |
||
| 197 | watchTasks.unshift("watch-composer:" + baseTask); |
||
| 198 | } |
||
| 199 | |||
| 200 | if (executeReleaseTasks) { |
||
| 201 | // Release tasks |
||
| 202 | const releaseFunction = function () { |
||
| 203 | const versionNumber = |
||
| 204 | config.useVersions === true |
||
| 205 | ? utils.getManifestVersion(dir + "/" + extensionName + ".xml") |
||
| 206 | : ""; |
||
| 207 | const versionName = ( |
||
| 208 | versionNumber !== "" |
||
| 209 | ? "-v" + versionNumber |
||
| 210 | : "" |
||
| 211 | ); |
||
| 212 | return gulp.src(sources) |
||
| 213 | .pipe(zip(zipName + versionName + ".zip")) |
||
| 214 | .pipe(gulp.dest(config.releaseDir + "/" + destinationReleaseDir)); |
||
| 215 | |||
| 216 | }; |
||
| 217 | if (composerExists) { |
||
| 218 | gulp.task("release-do:" + baseTask, releaseFunction); |
||
| 219 | gulp.task("release:" + baseTask, gulp.parallel(releaseTasks)); |
||
| 220 | } else { |
||
| 221 | gulp.task("release:" + baseTask, releaseFunction); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | if (executeWebTasks) { |
||
| 226 | // Clean task |
||
| 227 | gulp.task("clean:" + baseTask, function () { |
||
| 228 | return gulp.src(config.wwwDir + "/" + destinationWebDir, {"allowEmpty": true}) |
||
| 229 | .pipe(vinylPaths(function (paths) { |
||
| 230 | del.sync(paths, {"force": true}); |
||
| 231 | return Promise.resolve(); |
||
| 232 | })); |
||
| 233 | }); |
||
| 234 | |||
| 235 | // Copy tasks |
||
| 236 | gulp.task("copy-do:" + baseTask, gulp.series("clean:" + baseTask, function () { |
||
| 237 | return gulp.src(sources) |
||
| 238 | .pipe(gulp.dest(config.wwwDir + "/" + destinationWebDir)); |
||
| 239 | })); |
||
| 240 | gulp.task("copy:" + baseTask, gulp.series(copyTasks)); |
||
| 241 | |||
| 242 | // Watch tasks |
||
| 243 | const watchFunction = function () { |
||
| 244 | return gulp.watch(sources, |
||
| 245 | {interval: config.watchInterval}, |
||
| 246 | gulp.series("copy-do:" + baseTask)); |
||
| 247 | }; |
||
| 248 | |||
| 249 | if (composerExists) { |
||
| 250 | gulp.task("watch-composer:" + baseTask, function () { |
||
| 251 | return gulp.watch( |
||
| 252 | [dir + "/composer.json", dir + "/composer.lock"], |
||
| 253 | {interval: config.watchInterval}, |
||
| 254 | gulp.series("composer:" + baseTask) |
||
| 255 | ); |
||
| 256 | }); |
||
| 257 | gulp.task("watch-main:" + baseTask, watchFunction); |
||
| 258 | gulp.task("watch:" + baseTask, gulp.parallel(watchTasks)); |
||
| 259 | } else { |
||
| 260 | gulp.task("watch:" + baseTask, watchFunction); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | return generateTaskSet(baseTask, null, executeReleaseTasks, executeWebTasks); |
||
| 265 | } |
||
| 266 | |||
| 274 | exports.createGulpTasks = createGulpTasks; |